package com.scalar.chiptrack.operations.shippingRequest.actionCommands;

import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.operations.shippingRequest.dao.ShippingRequestDAO;
import com.scalar.chiptrack.operations.shippingRequest.forms.ShippingRequestForm;
import com.scalar.chiptrack.operations.shippingRequest.ShippingRequest;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * Created by ChandraSekahr.
 * User: Administrator
 * Date: Nov 17, 2005
 * Time: 7:11:52 PM
 */
public class ShippingRequestNewCommand extends ActionCommand
{
    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        ShippingRequestForm shippingRequestForm   = ( ShippingRequestForm ) form;
        ShippingRequest shippingRequest           = shippingRequestForm.getShippingRequest();
        ActionForward actionForward               = mapping.findForward( "newRequest" );;
        HttpSession mSession                      = request.getSession();
        HashMap errors                            = new HashMap();
        HashMap info                              = new HashMap();
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );

        try
        {
            Logger.debug( "In Shipping Request new command" );
            shippingRequestForm.setShippingRequest( new ShippingRequest() );
            //setting the request attributes
            request.setAttribute( "partNumberList", ShippingRequestDAO.getProductList( Constants.LOCATION_TYPE_CLASS_STORE ) );
            request.setAttribute( "vendorList", ShippingRequestDAO.loadClassStroeLocations( null )  );
            request.setAttribute( "shipToLocationList", ShippingRequestDAO.loadClassStroeLocations( null ) );

        }
        catch ( Exception e )
        {
            // Writes this error information onto the screen
            Logger.error( "Error while processing the request for creating a new shipping request", e );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, "Error while processing the request for creating a new shipping request" );

            // Prints more information in the pop-up
            mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while processing the request for creating a new shipping request", e ) );

            shippingRequestForm.setShippingRequest( new ShippingRequest() );
            request.setAttribute( "vendorList", ShippingRequestDAO.loadClassStroeLocations( null )  );
            request.setAttribute( "partNumberList", ShippingRequestDAO.getProductList( Constants.LOCATION_TYPE_CLASS_STORE ) );
            request.setAttribute( "yearList", ShippingRequestDAO.getYearList() );
            request.setAttribute( "statusList", ShippingRequestDAO.getStatusList() );
            actionForward = mapping.findForward( "search" );
        }

        return actionForward;

    }
}
